﻿using System.Collections.Generic;
using MSP.Data.Domain;
using MSP.Data.NHibernate;
using MSP.Data.Repositories;
using MSP.Business.Logic;
using MSP.Data.Domain.Enums;

namespace MSP.Business.UserManagement
{
    public class UserManager
    {
        public int SaveUser(User user)
        {
            int userId = 0;

            if (user != null)
            {
                if (user != null && user.Password != null && user.Password.Length < 40)
                {
                    user.Password = Tools.Encrypt(user.Password);
                }

                using (var unitOfWork = new UnitOfWork(NHibernateSessionFactory.SessionFactory))
                {
                    var userRepository = new UserRepository(unitOfWork.Session);
                    userRepository.Save(user);
                    unitOfWork.Commit();
                }

                userId = user.Id;
            }

            return userId;
        }

        public int SaveOrUpdate(User user)
        {
            int userId = 0;

            if (user != null)
            {
                if (user != null && user.Password != null && user.Password.Length < 40)
                {
                    user.Password = Tools.Encrypt(user.Password);
                }

                using (var unitOfWork = new UnitOfWork(NHibernateSessionFactory.SessionFactory))
                {
                    var userRepository = new UserRepository(unitOfWork.Session);
                    userRepository.SaveOrUpdateCopy(user);
                    unitOfWork.Commit();
                }

                userId = user.Id;
            }

            return userId;
        }

        public bool UpdateUser(User user)
        {
            bool result = false;

            if (user != null && user.Password != null && user.Password.Length < 40)
            {
                user.Password = Tools.Encrypt(user.Password);
            }

            using (var unitOfWork = new UnitOfWork(NHibernateSessionFactory.SessionFactory))
            {
                var userRepository = new UserRepository(unitOfWork.Session);
                result = userRepository.Update(user);
                unitOfWork.Commit();
            }

            return result;
        }

        public bool DeleteUser(User user)
        {
            bool result = false;
            using (var unitOfWork = new UnitOfWork(NHibernateSessionFactory.SessionFactory))
            {
                var userRepository = new UserRepository(unitOfWork.Session);
                result = userRepository.Delete(user);
                unitOfWork.Commit();
            }

            return result;
        }

        public User GetUserById(int Id)
        {
            var userRepository = new UserRepository(NHibernateSessionFactory.SessionFactory.OpenSession());
            User user = userRepository.GetUserById(Id);
            return user;
        }

        public IList<User> GetAllUser()
        {
            var userRepository = new UserRepository(NHibernateSessionFactory.SessionFactory.OpenSession());
            IList<User> users = userRepository.GetAllUser();

            return users;
        }

        public IList<KeyValuePair> GetAllUserStatu()
        {
            return KeyValuePair.ListFrom<UserStatusTypeEnum>();
        }

        public IList<KeyValuePair> GetAllUserGroup()
        {
            return KeyValuePair.ListFrom<UserGroupTypeEnum>();
        }

    }
}
